package ylock

import (
	"sync"
	"sync/atomic"
)

var _ sync.Locker = (*SpinLock)(nil)

type SpinLock struct {
	state int32
}

func (s *SpinLock) Lock() {
	for !atomic.CompareAndSwapInt32(&s.state, 0, 1) {
		// 如果 CAS 操作失败（即锁已被占用），就自旋
	}
}

func (s *SpinLock) Unlock() {
	atomic.StoreInt32(&s.state, 0) // 释放锁
}
